/****************************************************************************************

 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>

 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>

 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>

 ****************************************************************************************/

package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FloatingPopulationModeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

/**
 * <Description> <br>
 *
 * @author liaojing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年11月22日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(FloatingPopulationModeAttrHandler.class)
public class FloatingPopulationModeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();

        KiteAttr settleAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004108");
        //销售价属性处理

        KiteAttr sellAttr = querySceneInstService.getInstAttr(custId,InstType.PROD_INST, prodInstId,"200004107");

        String sceneInstId = data.getSceneInstId();
        //型号对应的结算价
        if (Objects.nonNull(settleAttr)) {
            //雁飞流动人口房屋管理产品-A2（含集中运维100个起订）-999元
            setHandleAttrValue ("110010243", attrValue, inst, "200004108", "999", sceneInstId);
            //key:型号枚举值ID  newFieldName:结算价的属性编码   newValue:结算价的枚举值ID

            //雁飞流动人口房屋管理产品-A2（含单个运维20个起订）-1099元
            setHandleAttrValue ("110010244", attrValue, inst, "200004108", "1099", sceneInstId);

            //雁飞流动人口房屋管理产品-B180（含集中运维100个起订）-1300元
            setHandleAttrValue ("110010245", attrValue, inst, "200004108", "1300", sceneInstId);

            //雁飞流动人口房屋管理产品-B180（含单个运维20个起订）-1400元
            setHandleAttrValue ("110010246", attrValue, inst, "200004108", "1400", sceneInstId);
        }

        //型号对应的销售价
        if(Objects.nonNull(sellAttr)){
            //雁飞流动人口房屋管理产品-A2（含集中运维100个起订）-1200元
            setHandleAttrValue ("110010243", attrValue, inst, "200004107", "1200", sceneInstId);

            //雁飞流动人口房屋管理产品-A2（含单个运维20个起订）-1300元
            setHandleAttrValue ("110010244", attrValue, inst, "200004107", "1300", sceneInstId);

            //雁飞流动人口房屋管理产品-B180（含集中运维100个起订）-1500元
            setHandleAttrValue ("110010245", attrValue, inst, "200004107", "1500", sceneInstId);

            //雁飞流动人口房屋管理产品-B180（含单个运维20个起订）-1600元
            setHandleAttrValue ("110010246", attrValue, inst, "200004107", "1600", sceneInstId);
        }

        // 联动同场景下其他产品的型号
        setOthProdInstMode(attrValue, inst, sceneInstId);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        // TODO Auto-generated method stub

    }

    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
        if (KiteStringUtils.isEqual(key, oldValue)) {
            operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
        }
    }
    private void setOthProdInstMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 找到型号
            KiteAttr kiteAttr = prodInst.getAttr("200002772");
            if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId())) {
                List<KiteAttrValue> kiteAttrValue = kiteAttr.getAttrValues();
                //value是主控板型号和其它产品或附加商品的型号相同时，联动一致
                List<KiteAttrValue> attrValues = kiteAttrValue.stream().filter(AttrValue -> KiteStringUtils.equalsAny(AttrValue.getAttrValue(), value)).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(attrValues)) {
                    if(KiteStringUtils.equalsAny(prodInst.getSpecId(),"63249366","63249555","63249367")){
                        operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), value, sceneInstId, null);
                    }
                }
            }
        });
    }
}
